<?php TPL::output('admin/global/header.tpl.htm'); ?>
<!--left-->
<?php TPL::output('admin/global/left.tpl.htm'); ?>
<!--left-->
<!-- main -->

<div id="main-content">
	<div class="clear"></div>
	<div class="content-box">
		<div class="content-box-header">
			<h3><?php echo $this->crumb['last']['name']; ?></h3>
			<ul class="content-box-tabs">
				<li><a href="#tab1" class="default-tab current">新闻列表</a></li>
				<li><a class="" href="#tab2">添加新闻</a></li>
			</ul>
			<div class="clear"></div>
		</div>
		<div class="content-box-content">
			<div class="tab-content default-tab" id="tab1">
				<?php if($this->list){ ?>
				<table class="list">
					<thead>
						<tr>
							<th style="width:40px;" align="center">ID</th>
							<th>新闻标题</th>
							<th>新闻链接</th>
							<th>操作</th>
						</tr>
					</thead>
					<tfoot>
						<tr>
							<td colspan="6"><div class="pagination"> <?php echo $this->pagination; ?> </div>
								<div class="clear"></div></td>
						</tr>
					</tfoot>
					<tbody style="border-bottom: 1px solid #ddd;">
						<?php foreach ($this->list AS $key => $val) { ?>
						<tr style="vertical-align:middle;">
							<td align="center"><?php echo $val['id']; ?></td>
							
							<td > 
							<input id ="news_title_<?php echo $val['id'] ?>" value ="<?php echo $val['title']; ?>" style ="display:none;" />
							<a id ="news_title_b_<?php echo $val['id'] ?>" href="<?php echo 'http://'.$val['url']; ?>" target="_blank"><?php echo $val['title']; ?></a>
							
							</td>
							
							<td>
							<input id ="news_url_<?php echo $val['id'] ?>" value ="<?php echo $val['url']; ?>" style ="display:none;" />
							<a id ="news_url_b_<?php echo $val['id'] ?>" href="<?php echo 'http://'.$val['url']; ?>" target="_blank"><?php echo $val['url']; ?></a>
							
							</td>
							
							<td>
							<div id ="news_edit_<?php echo $val['id'] ?>"><a  href="javascript:" onclick ="edit(<?php echo $val['id'] ?>)" title="编辑新闻">编辑</a> &nbsp; </div>
							<div id ="news_save_<?php echo $val['id'] ?>"  style ="display:none;"><a  href="javascript:" onclick ="save(<?php echo $val['id'] ?>)" title="保存新闻">保存</a>  &nbsp;</div>
							<a href="javascript:" onclick ="del(<?php echo $val['id'] ?>)" title="删除新闻">删除</a>  &nbsp;
						</tr>
						<?php } ?>
					</tbody>
				</table>
				<?php } ?>
			</div>
			<div class="tab-content" id="tab2" style="display: none;">
				<div id="notification_box" class="notification success png_bg" style="display:none;"> <a class="close" href="javascript:;"></a>
					<div name="notification_content"></div>
				</div>
				<h3>添加新闻</h3>
				<form id="_save_form">
					<p>
						<label>新闻标题：</label>
						<input id  ="news_add_title" type="text" value="" class="text-input medium-input" name="title">
					</p>
					<p>
						<label>目标链接：</label>
						<input id ="news_add_url" type="text" class="text-input medium-input">
					</p>
					
					<p>
						<input class="button" type="button" onClick="add()" value="添加新新闻" />
					</p>
				</form>
			</div>
		</div>
	</div>
	<!-- footer -->
	<?php TPL::output('admin/global/footer.tpl.htm'); ?>
	<!-- footer -->
</div>
<!-- main -->
</div>

<script>

function edit(id)
{
	$('#news_title_'+id).css('display','block');
	$('#news_title_b_'+id).css('display','none');
	
	$('#news_url_'+id).css('display','block');
	$('#news_url_b_'+id).css('display','none');
	
	$('#news_save_'+id).css('display','block');
	$('#news_edit_'+id).css('display','none');
}

function save(id)
{
	
	var arg = {
	
	'title':$('#news_title_'+id).val(),
	'url':$('#news_url_'+id).val(),
	'id':id
			
	};
 	$.post('?/admin/news/ajax_save/',{arg:arg},function(html){
		console.log(html);
		if(html == '1')
		{
			alert('保存成功');
			location.reload();
		}
		else
		{
		   alert('保存失败');
		   location.reload();
		} 
		
		
	});	
	
}

function del(id)
{
	$.post('?/admin/news/ajax_del/',{news_id:id},function(html){
		console.log(html);
		if(html == '1')
		{
			alert('删除成功');
			location.reload();
		}
		else
		{
		   alert('删除失败');
		   location.reload();
		} 
		
		
	});	
}

function add(id)
{
	var arg = {
			'question_id':'<?php echo $this->question_id ?>',
			'title':$('#news_add_title').val(),
			'url':$('#news_add_url').val(),		
			};
	
	$.post('?/admin/news/ajax_add/',{arg:arg},function(html){
		console.log(html);
		if(html == '1')
		{
			alert('添加成功');
			location.reload();
		}
		else
		{
		   alert('添加失败');
		   location.reload();
		} 
		
		
	});	
}



</script>
</body></html>